/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月10日         luwei
 */

package com.tianci.appstore.ui.list;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.bootsetting.LauncherOnBootLayout;
import com.tianci.appstore.ui.widget.CatchImageView;

public class ListSelectedBlock extends ListBlock {
    private TextView mEmpty = null;

    public ListSelectedBlock(Context context, ListType type) {
        super(context, type);
    }

    @Override
    public void update(SkyAppBaseData data) {
        this.data = (SkyListAppData) data;
        if (this.data.getPkgName().equals("")) {
            addEmptyTips();
            iconImage.setVisibility(View.GONE);
            rightLayout.setVisibility(View.GONE);
        } else {
            iconImage.setVisibility(View.VISIBLE);
            rightLayout.setVisibility(View.VISIBLE);
            super.update(data);
            if (mEmpty != null) {
                mEmpty.setVisibility(View.GONE);
            }
        }
        if (this.data.getPkgName().equals(LauncherOnBootLayout.mStartApp.getPkgName())
                && this.data.getMainActivity().equals(
                LauncherOnBootLayout.mStartApp.getMainActivity())) {
            initTickImage();
        } else {
            if (tickImage != null) {
                tickImage.setVisibility(View.GONE);
            }
        }
    }

    private void initTickImage() {
        if (tickImage == null) {
            tickDrawable = SkinManager.getResDrawable(R.drawable.ticked);
            tickImage = new CatchImageView(mContext);
            tickImage.setImageDrawable(tickDrawable);
            this.addView(tickImage, new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.BOTTOM));
        }
        tickImage.setVisibility(View.VISIBLE);
    }

    private void addEmptyTips() {
        if (mEmpty == null) {
            mEmpty = new TextView(mContext);
            mEmpty.setTextColor(Color.WHITE);
            mEmpty.setTextSize(titleSize);
            mEmpty.setText(R.string.launch_on_boot_empty);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            addView(mEmpty, params);
        }
        mEmpty.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        // ((Activity)mContext).finish();
        try {
            String result = LiteContentServiceConnecter.getService().call(
                    "setting://com.coocaa.app/startup_app?action=set&pkgname=" + data.getPkgName()
                            + "&activity=" + data.getMainActivity());
            if (result != null) {
                JSONObject object = JSONObject.parseObject(result);
                int code = object.getInteger("error_code");
                if (code == 0) {
                    ((Activity) mContext).finish();
                } else {

                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // super.onClick(v);
    }

}
